﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WCSY.MODEL;
using WCSY.BLL;

public partial class Components_SystemMenus_MenuLeft : System.Web.UI.Page
{
    
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["parentid"]!=null)
        {
            string roleId = "";
            if (Request["roleid"] != null)
            {
                roleId = Request["roleid"].ToString();
            }
            if (roleId.Equals(""))
            {
                this.RegisterStartupScript("close", "<script>chkLogin()</script>");
            }   
        int parentid = Convert.ToInt32(Request["parentid"].ToString());
        pareentNodeTree(parentid,roleId);
        string parentname = Request["parentname"].ToString();
        Guide_top.Controls.Add(new LiteralControl("<div id=\"Guide_toptext\">" + parentname + "</div>"));
        
        }
        
    }
    //public void childNodes(int parentid)
    //{
        
    //    string sql = "select rightsid,parentid,rightsname,rightstype,rightsurl,remarks from mg_rights where parentid = " + Convert.ToInt32(parentid) + " and rightstype=1";
    //    List<Mg_right> rights = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;

    //    //childNode.SelectAction = TreeNodeSelectAction.Expand;

    //    foreach (Mg_right c in rights)
    //    {
    //        //TreeNode child = new TreeNode();
    //        //child.Text = c.Rightsname;
    //        //child.Value = c.Rightsid.ToString();

    //        Guide_box.Controls.Add(new LiteralControl("<div class=\"guideexpand\" onclick=\"Switch(this)\">" + c.Rightsname + "</div>"));//添加<div>标签


            
    //        //childNode.ChildNodes.Add(child);
    //        ///child.ImageUrl = "~/image/folder1.gif";
    //        //if (!c.Rightsurl.Equals(""))
    //        //{
    //        //    child.NavigateUrl = "JAVAscript:openUrl('" + c.Rightsurl + "');";
    //        //}
    //        //child.NavigateUrl = c.Rightsurl;

    //        pareentNodeTree(c.Rightsid);
    //    }
    //}

    //public void childNodesEx(int parentid)
    //{
    //    string sql = "select rightsid,parentid,rightsname,rightstype,rightsurl,remarks from mg_rights where parentid = " + parentid + " and rightstype=1";
    //    List<Mg_right> rights = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;
    //    //判断该rightsid 是否还有下级菜单,如果有，则继续循环二级菜单
    //    //foreach (Mg_right c in rights)
    //    //{
    //    //    string issql = "select rightsid,parentid,rightsname,rightstype,rightsurl,remarks from mg_rights where parentid = " + c.Rightsid + " and rightstype=1";
    //    //    List<Mg_right> isrights = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;
    //    //    if(isrights.Count>0){
    //    //        Guide_box.Controls.Add(new LiteralControl("<div class=\"guide\" onclick=\"Switch(this)\">" + c.Rightsname + "</div>"));//添加<div>标签
    //    //    }
        
    //    //}
    //    //childNode.SelectAction = TreeNodeSelectAction.Expand;
    //    Guide_box.Controls.Add(new LiteralControl("<div class=\"guide\">"));
    //    Guide_box.Controls.Add(new LiteralControl("<ul>"));
    //    foreach (Mg_right c in rights)
    //    {
    //        //TreeNode child = new TreeNode();
    //        //child.Text = c.Rightsname;
    //        //child.Value = c.Rightsid.ToString();5
    //        Guide_box.Controls.Add(new LiteralControl("<li>"));

    //        Guide_box.Controls.Add(new LiteralControl("<a "));
    //        if(!c.Rightsurl.Equals("")){
    //            Guide_box.Controls.Add(new LiteralControl("href=\"javascript:topage('../../" + c.Rightsurl + "')\" "));
    //        }
            
    //        Guide_box.Controls.Add(new LiteralControl(">" + c.Rightsname + "</a></li>"));
    //    }
    //    Guide_box.Controls.Add(new LiteralControl("</ul>"));
    //    Guide_box.Controls.Add(new LiteralControl("</div>"));
    //}
    
    //插入tree 二级菜单后
    public void pareentNodeTree(int parentid, string roleId)
    {
        string sql = "select a.rightsid,a.parentid,a.rightsname,a.rightstype,a.rightsurl,a.remarks" +
       " from  mg_rights as a,mg_rrrelation as b " +
       "  where a.rightsid=b.rightsid  and a.rightstype=1 " +
       "  and parentid=" + parentid + " and b.roleid='" + roleId + "' order by a.rightsid";
        List<Mg_right> rights = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;
        //Rights.GetChildRight(tnode.Value);
        TreeView1.Nodes.Clear();

        foreach (Mg_right t in rights)
        {
            TreeNode root = new TreeNode();
            string rootValue = t.Rightsname;
            int rootId = t.Rightsid;
            root.Text = rootValue;
            root.Value = rootId.ToString();


            TreeView1.Nodes.Add(root);
            root.ImageUrl = "~/image/base1.gif";
            if (!t.Rightsurl.Equals(""))
            {
                root.NavigateUrl = "JAVAscript:topage('../../" + t.Rightsurl + "');";
            }
            childNodes(root,roleId);
        }
    }

    //插入treeview 子菜单
    public void childNodes(TreeNode childNode,string roleId)
    {
 
        //string sql = "select rightsid,parentid,rightsname,rightstype,rightsurl,remarks from mg_rights where parentid = " + Convert.ToInt32(childNode.Value) + " and rightstype=1";
        string sql = "select a.rightsid,a.parentid,a.rightsname,a.rightstype,a.rightsurl,a.remarks" +
      " from  mg_rights as a,mg_rrrelation as b " +
      "  where a.rightsid=b.rightsid  and a.rightstype=1 " +
      "  and parentid=" + Convert.ToInt32(childNode.Value) + " and b.roleid='" + roleId + "'  order by a.rightsid";
        List<Mg_right> rights = Mg_rightManager.GetMg_rightsBySql(sql) as List<Mg_right>;

        childNode.SelectAction = TreeNodeSelectAction.Expand;

        foreach (Mg_right c in rights)
        {
            TreeNode child = new TreeNode();
            child.Text = c.Rightsname;
            child.Value = c.Rightsid.ToString();

            childNode.ChildNodes.Add(child);
            child.ImageUrl = "~/image/base1.gif";
            if (!c.Rightsurl.Equals(""))
            {
                //child.NavigateUrl = "JAVAscript:openUrl('" + c.Rightsurl + "');";//href=\"javascript:topage('../../" + c.Rightsurl + "')\" 
                child.NavigateUrl = "JAVAscript:topage('../../" + c.Rightsurl + "');";
            }
            //child.NavigateUrl = c.Rightsurl;

            childNodes(child,roleId);
        }


    }

       
}
